<?php

namespace common\components;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * 全局模型约束
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class ModelConstraint extends \yii\base\Component {

    public static $remotes = [
        1 => '', //shop
        2 => 'dynamic',
        3 => 'match',
        4 => 'game',
        5 => 'activity',
    ];
    public static $apis = [
        'activity' => ['/shop/activity/view'],
        'assemble' => ['/shop/assemble/index'],
    ];

    public static function link($model, $model_id, $type) {
        if (!$type) {
            return '';
        }
        $route = ArrayHelper::getValue(self::$remotes, $type);
        if ($route === null) {
            return $route;
        }
        return Url::h5(rtrim($route, '/') . '/' . $model . '/' . $model_id);
    }

    public static function api($model, $model_id) {
        $route = ArrayHelper::getValue(self::$apis, $model);
        if ($route === null) {
            return $route;
        }
        $route['id'] = $model_id;
        return ltrim(Url::to($route), '/');
    }

}
